package com.egao.common.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.system.entity.Layout;
import com.egao.common.module.system.mapper.LayoutMapper;
import com.egao.common.module.system.service.LayoutService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 网页管理接口实现类
 * @author pyx
 */
@Service
public class LayoutServiceImpl extends ServiceImpl<LayoutMapper, Layout> implements LayoutService {

    @Override
    public List<Layout> listPage(PageParam<Layout> pageParam) {
        List<Layout> layouts = baseMapper.listPage(pageParam);
        return layouts;
    }

    @Override
    public JsonResult insertLayout(Layout layout) {
        if(baseMapper.insert(layout) == 1){
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Override
    public JsonResult updateLayout(Layout layout) {
        if(baseMapper.updateById(layout) == 1){
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult deleteLayout(Integer id) {
        if(baseMapper.deleteById(id) == 1){
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult get(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        Layout layout = baseMapper.selectById(id);

        if(layout == null){
            throw new ParameterException("网页不存在");
        }

        return JsonResult.ok().setData(layout);
    }
}
